package bookstudytest.singleton;

import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: Lee
 * @Date: 2018/6/11 10:45
 * @Description:
 */
public class LazyTest implements Runnable {


    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            //速度还好
//            for (int n = 0; n < 2000; n++) {
//                Thread a = new Thread(() -> new LazyTest().getLazy());
//                a.start();
//            }
            //有时候慢
//            for (int n = 0; n < 20000; n++) {
//                Thread a = new Thread(() -> new LazyTest().getLazyLock());
//                a.start();
//            }
            //比较慢，有时候很慢
//            for (int n = 0; n < 20000; n++) {
//                Thread a = new Thread(() -> new LazyTest().getLazyDoubleLock());
//                a.start();
//            }
//            //比较快
//           for (int n = 0; n < 20000; n++) {
//                Thread a = new Thread(() -> new LazyTest().getLazyInnerClass());
//                a.start();
//            }
            //也比较慢
//           for (int n = 0; n < 20000; n++) {
//                Thread a = new Thread(() -> new LazyTest().getHungerClass());
//                a.start();
//            }
            //比较快
            for (int n = 0; n < 20000; n++) {
                Thread a = new Thread(() -> new LazyTest().getEnumClass());
                a.start();
            }
        }

    }

    @Override
    public void run() {
        //多线程生成时在执行构造方法时会生成好几次LazyDemo的实例
        LazyDemo lazyDemo = LazyDemo.getInstance();
    }

    //    不安全
    public void getLazy() {
        LazyDemo lazyDemo = LazyDemo.getInstance();
    }

    //不安全
    public void getLazyLock() {
        LazyLockDemo lazyLockDemo = LazyLockDemo.getInstance();
    }

    //不安全
    public void getLazyDoubleLock() {
        LazyDoubleLockDemo lazyDoubleLockDemo = LazyDoubleLockDemo.getInstance();
    }

    //不安全
    public void getLazyInnerClass() {
        LazyInnerClassDemo lazyInnerClassDemo = LazyInnerClassDemo.getInstance();
    }

    //安全
    public void getHungerClass() {
        HungerDemo hungerDemo = HungerDemo.getInstance();
    }

    //安全
    public void getEnumClass() {
        EnumDemo enumDemo = EnumDemo.INSTANCE;

    }
}
